package com.miwzn.boot.modules.sys.service.impl;

import com.miwzn.base.AbstractService;
import com.miwzn.boot.component.config.shiro.ShiroUser;
import com.miwzn.boot.modules.sys.entity.po.SysMenu;
import com.miwzn.comm.constant.MyConstant;
import com.miwzn.comm.utils.CommUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.miwzn.boot.modules.sys.mapper.SysMenuMapper;
import com.miwzn.boot.modules.sys.service.SysMenuService;
import com.miwzn.boot.modules.sys.entity.qm.SysMenuQuery;
import com.miwzn.boot.modules.sys.entity.dto.SysMenuDto;

import java.util.*;

/**
 * @Author: wzn
 * @Date: 2018-12-06 14:02:06
 * @Description: www.miwzn.com service  实现
 */
@Service
public class SysMenuServiceImpl extends AbstractService<SysMenu, SysMenuDto, SysMenuQuery> implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenu> selectNav() {
        final ShiroUser loginUser = CommUtil.getLoginUser();
        SysMenu q = new SysMenu();
        q.setDelFlag(MyConstant.FLAG_N);
        if (loginUser.isSuperAdmin()) {
            return sysMenuMapper.select(q);
        } else {
            List<SysMenu> sysMenus = sysMenuMapper.selectNav(loginUser.getId(), loginUser.getRole() == null ? null : loginUser.getRole().getId());
            if (CollectionUtils.isEmpty(sysMenus)) return null;
            Set<String> ids = new HashSet<>();
            for (SysMenu sysMenu : sysMenus) {
                ids.add(sysMenu.getId());
                String[] parentIds = sysMenu.getParentIds().split(",");
                ids.addAll(Arrays.asList(parentIds));
            }
            return sysMenuMapper.selectNavByIds(ids);
        }
    }
}
